package dao;

import org.apache.ibatis.annotations.*;
import pojo.User;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "password", property = "password"),
            @Result(column = "real_name", property = "realName"),
            @Result(column = "user_role", property = "userRole"),
            @Result(column = "last_login", property = "lastLogin"),
            @Result(column = "login_count", property = "loginCount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "role_name", property = "roleName")
    })
    List<User> findAll();

    @Select("SELECT u.*, r.role_name " +
            "FROM user u " +
            "LEFT JOIN user_role r ON u.user_role = r.role_id")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "password", property = "password"),
            @Result(column = "real_name", property = "realName"),
            @Result(column = "user_role", property = "userRole"),
            @Result(column = "last_login", property = "lastLogin"),
            @Result(column = "login_count", property = "loginCount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "role_name", property = "roleName")
    })
    List<User> findAllWithRole();

    @Select("SELECT * FROM user WHERE user_id = #{userId}")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "password", property = "password"),
            @Result(column = "real_name", property = "realName"),
            @Result(column = "user_role", property = "userRole"),
            @Result(column = "last_login", property = "lastLogin"),
            @Result(column = "login_count", property = "loginCount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "role_name", property = "roleName")
    })
    User findById(int userId);

    @Select("SELECT * FROM user WHERE user_name = #{userName}")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "telephone", property = "telephone"),
            @Result(column = "password", property = "password"),
            @Result(column = "real_name", property = "realName"),
            @Result(column = "user_role", property = "userRole"),
            @Result(column = "last_login", property = "lastLogin"),
            @Result(column = "login_count", property = "loginCount"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "role_name", property = "roleName")
    })
    User findByUserName(String userName);

    @Insert("INSERT INTO user(" +
            "user_name, telephone, password, real_name, user_role, login_count) " +
            "VALUES(#{userName}, #{telephone}, #{password}, #{realName}, #{userRole}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int addUser(User user);

    @Update("UPDATE user SET " +
            "telephone = #{telephone}, " +
            "real_name = #{realName}, " +
            "user_role = #{userRole} " +
            "WHERE user_id = #{userId}")
    int updateUserInfo(User user);

    @Update("UPDATE user SET " +
            "password = #{password} " +
            "WHERE user_id = #{userId}")
    int updatePassword(@Param("userId") int userId, @Param("password") String password);

    @Update("UPDATE user SET " +
            "last_login = NOW(), " +
            "login_count = login_count + 1 " +
            "WHERE user_id = #{userId}")
    int updateLoginInfo(int userId);

    @Delete("DELETE FROM user WHERE user_id = #{userId}")
    int deleteUser(int userId);
}